package com.wjc.wiki.job;

import com.wjc.wiki.service.EbookSnapshotService;
import com.wjc.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class EbookSnapshotJob {
    @Resource
    private EbookSnapshotService ebookSnapshotService;

    @Resource
    private SnowFlake snowFlake;

   private static final Logger LOG = LoggerFactory.getLogger(EbookSnapshotJob.class);


   /**
    * 自定义cron表达式跑批
    * 只有等上一次执行完成，下一次才会在下一个时间点执行，错过就错过
    *  每10分钟获取电子书快照
    */

   @Scheduled(cron = "0 1/10 * * * ?")
   public void cron()  {
       // 增加日志流水号
       MDC.put("LOG_ID", String.valueOf(snowFlake.nextId()));

       LOG.info("==========定时获取电子书快照开始===========");
       long start=System.currentTimeMillis();
       ebookSnapshotService.genSnapshot();
       LOG.info("定时获取电子书快照耗时: {}毫秒",System.currentTimeMillis()-start);
       LOG.info("==========定时获取电子书快照===========");
   }

}
